<script setup>
import EvaluationItem from "../components/EvaluationItem.vue";
import { ref, onMounted } from "vue";
import { getCommentList } from "@/api/user.js";
import { useUser } from "@/store/useUser.js";

const userStore = useUser();
const userInfo = userStore.getUserInfo;
const commentType = ref(null);
const orderBy = ref(0);
const pageSize = ref(5);
const pageNum = ref(1);

const totalCount = ref(0);
const goodCount = ref(0);
const neutralCount = ref(0);
const negativeCount = ref(0);

const commentList = ref([]);

const getCommentTypeList = (type) => {
  commentType.value = type;
  pageNum.value = 1;
  getComments();
};

const activeSort = (type) => {
  orderBy.value = type;
  pageNum.value = 1;
  getComments();
};

const currentChange = (page) => {
  pageNum.value = page;
  getComments();
};

// 获取评价列表
const loading = ref(false);
const getComments = async () => {
  try {
    loading.value = true;
    const params = {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      type: commentType.value,
      orderBy: orderBy.value,
      // 注意: saleUserId 需要从用户状态或其他地方获取
      saleUserId: userInfo.userId,
    };
    if (!params.type) delete params.type;
    const res = await getCommentList(params);
    if (res.code === 200) {
      commentList.value = res.data.commentList;
      if (!totalCount.value) totalCount.value = Number(res.data.totalCount);
      if (!goodCount.value) goodCount.value = Number(res.data.goodCount);
      neutralCount.value = Number(res.data.neutral_count) || 0;
      negativeCount.value = Number(res.data.negative_count) || 0;
    } else {
      // 处理错误
      console.error(res.msg);
    }
  } catch (error) {
    console.error("获取评价列表失败", error);
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  getComments();
});
</script>

<template>
  <h3>我的评价</h3>
  <div class="evaluate-list">
    <div class="evaluate-page">
      <div class="header">
        <p class="title">获得评价（{{ totalCount || 0 }}）</p>
        <div class="sort-box">
          <p class="filter">
            <span
              :class="{ active: !commentType }"
              @click="getCommentTypeList(0)"
              >全部({{ totalCount || 0 }})</span
            >
            <span
              :class="{ active: commentType === 1 }"
              @click="getCommentTypeList(1)"
              >好评({{ goodCount || 0 }})</span
            >
            <span
              :class="{ active: commentType === 2 }"
              @click="getCommentTypeList(2)"
              >中评({{ neutralCount || 0 }})</span
            >
            <span
              :class="{ active: commentType === 3 }"
              @click="getCommentTypeList(3)"
              >差评({{ negativeCount || 0 }})</span
            >
          </p>
          <div class="sort">
            <span :class="{ active: orderBy === 0 }" @click="activeSort(0)"
              >默认排序</span
            >
            <span :class="{ active: orderBy === 1 }" @click="activeSort(1)"
              >最新评价</span
            >
          </div>
        </div>
      </div>
      <div v-loading="loading">
        <EvaluationItem
          v-for="item in commentList"
          :key="item.id"
          :itemData="item"
        />
      </div>
      <div class="paging-box">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pageSize"
          :total="totalCount"
          :current-page="pageNum"
          @current-change="currentChange"
        />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.evaluate-list {
  flex: 1;
  background: #ffffff;
  border-radius: 16px 16px 16px 16px;
  border: 1px solid rgba(25, 30, 40, 0.08);
  margin-top: 16px;
  overflow-y: auto;
  padding: 40px;
  box-sizing: border-box;
  .evaluate-page {
    max-width: 1200px;
  }
  .header {
    .title {
      color: #191e28;
      font-size: 20px;
      font-weight: bold;
      margin-bottom: 20px;
    }
    .sort-box {
      display: flex;
      align-items: center;
      span {
        line-height: 12px;
        cursor: pointer;
        &:nth-child(1) {
          padding-right: 12px;
          margin-right: 12px;
          border-right: 1px solid rgba(25, 30, 40, 0.08);
        }
      }
      .sort {
        color: #191e28;
      }
      .active {
        color: #1dbf73;
        font-weight: bold;
      }
    }
    .filter {
      flex: 1;
      margin-bottom: 20px;
      span {
        padding: 4px 8px;
        color: rgba(25, 30, 40, 0.6);
        background: rgba(25, 30, 40, 0.02);
        border-radius: 6px 6px 6px 6px;
        margin-right: 12px;
        &.active {
          color: #1dbf73;
          background: rgba(29, 191, 115, 0.08);
        }
      }
    }
  }
}
.paging-box {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 20px;
}
</style>
